package com.gopay.scheduler.shedulerx.listener;

import com.gopay.common.constants.scheduler.SchedulerConstants;
import com.gopay.scheduler.shedulerx.SchedulerConfigLoad;
import com.gopay.scheduler.shedulerx.SchedulerOperateFactory;
import com.gopay.scheduler.shedulerx.alarm.AlarmEventBus;
import com.gopay.scheduler.shedulerx.event.JobEventBus;
import com.gopay.scheduler.shedulerx.event.TriggerMisfireEvent;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 19371
 */
public class GopaySchedulerListener {

     private final static Logger LOGGER = LoggerFactory.getLogger(GopaySchedulerListener.class);

    private volatile static GopaySchedulerListener listener;

    private  final ListenerManager listenerManager;

    private GopaySchedulerListener(ListenerManager listenerManager) {
        this.listenerManager = listenerManager;
    }

    public static GopaySchedulerListener getSingle(ListenerManager listenerManager){
        if(listener == null){
            synchronized (GopaySchedulerListener.class){
                if(listener == null){
                    listener = new GopaySchedulerListener(listenerManager);
                }
            }
        }
        return listener;
    }

    public void init(){
        listenerManager.addTriggerListener(new ListenTrigger());
        listenerManager.addSchedulerListener(new ListenScheduler());
    }


    class ListenTrigger implements TriggerListener{

        @Override
        public String getName() {
            return "Scheduler-ListenTrigger";
        }

        @Override
        public void triggerFired(Trigger trigger, JobExecutionContext context) {

        }

        @Override
        public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
            return false;
        }

        @Override
        public void triggerMisfired(Trigger trigger) {
            Trigger.TriggerState triggerState = null;
            try {
                triggerState = SchedulerOperateFactory.getFactory().getScheduler().getTriggerState(trigger.getKey());
            } catch (SchedulerException e) {
                LOGGER.error(" triggerMisfired occur error :",e);
            }
            if(Trigger.TriggerState.ERROR.equals(triggerState) || Trigger.TriggerState.BLOCKED.equals(triggerState) ) {
                TriggerMisfireEvent event = new TriggerMisfireEvent();
                event.setEventType(SchedulerConstants.SCHEDULER_MISFIRE_EVENT);
                event.setTriggerState(triggerState.name());
                event.setJobName(trigger.getJobKey().getName());
                event.setRegion(SchedulerOperateFactory.getFactory().getRegionMapping().get(event.getJobName()));
                event.setDecription(SchedulerOperateFactory.getFactory().getJobDescription().get(trigger.getJobKey().getName()));
                SchedulerOperateFactory.getFactory().postMisfireEvent(event);
            }

        }

        @Override
        public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {

        }
    }

     class ListenJob implements JobListener{

        @Override
        public String getName() {
            return "Scheduler-ListenJob";
        }

        @Override
        public void jobToBeExecuted(JobExecutionContext context) {

        }

        @Override
        public void jobExecutionVetoed(JobExecutionContext context) {

        }

        @Override
        public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {

        }
    }

     class ListenScheduler implements SchedulerListener {

        @Override
        public void jobScheduled(Trigger trigger) {
        }

        @Override
        public void jobUnscheduled(TriggerKey triggerKey) {

        }

        @Override
        public void triggerFinalized(Trigger trigger) {
        }

        @Override
        public void triggerPaused(TriggerKey triggerKey) {
        }

        @Override
        public void triggersPaused(String triggerGroup) {
        }

        @Override
        public void triggerResumed(TriggerKey triggerKey) {
        }

        @Override
        public void triggersResumed(String triggerGroup) {
        }

        @Override
        public void jobAdded(JobDetail jobDetail) {

        }

        @Override
        public void jobDeleted(JobKey jobKey) {

        }

        @Override
        public void jobPaused(JobKey jobKey) {
        }

        @Override
        public void jobsPaused(String jobGroup) {
        }

        @Override
        public void jobResumed(JobKey jobKey) {
        }

        @Override
        public void jobsResumed(String jobGroup) {
        }

        @Override
        public void schedulerError(String msg, SchedulerException cause) {
        }

        @Override
        public void schedulerInStandbyMode() {

        }

        @Override
        public void schedulerStarted() {
        }

        @Override
        public void schedulerStarting() {
        }

        @Override
        public void schedulerShutdown() {
            LOGGER.info("----------------->shutdown the scheduler resource<----------------------");
            SchedulerOperateFactory.getFactory().shutdown();
        }

        @Override
        public void schedulerShuttingdown() {
        }

        @Override
        public void schedulingDataCleared() {

        }
    }
}
